package com.java2104.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.java2104.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getInfo")
    //如果希望返回对象的JSON格式，需要在方法前添加 @ResponseBody
    @ResponseBody
    @JsonView(User.WithPasswordView.class)
    public User getInfo() {
        //假设这个对象是借助mybatis从数据库中查询到的
        User user = new User();
        user.setUsername("liudehua520");
        user.setPassword("567890");
        user.setNickName("刘德华");
        user.setState(1);
        user.setLastLogin(new Timestamp(System.currentTimeMillis()));
        return user;
    }

    @RequestMapping("/getInfo2")
    //如果希望返回对象的JSON格式，需要在方法前添加 @ResponseBody
    @ResponseBody
    @JsonView(User.WithoutPasswordView.class)
    public User getInfo2() {
        User user = new User();
        user.setUsername("liudehua520");
        user.setPassword("567890");
        user.setNickName("张学友");
        user.setState(1);
        user.setLastLogin(new Timestamp(System.currentTimeMillis()));
        return user;
    }

    @RequestMapping("/updatePassword")
    // localhost:8080/user/updatePassword?user_id=1002&old_pass=123456&new_pass=567890
    public String updatePassword(Model model, @RequestParam("user_id") String userId,
                                 @RequestParam("old_pass") String oldPass,
                                 @RequestParam("new_pass") String newPass,
                                 @RequestAttribute("token") String token) {
        model.addAttribute("userId", userId);
//        model.addAttribute("userId", userId);
        model.addAttribute("newPass", newPass);

        model.addAttribute("token", token);

        return "updatePassword";
    }
}
